package economy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import main.QConomy;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.metadata.FixedMetadataValue;

public class EconomyLoader {
	private Connection connection;
	private final QConomy plugin;

	public EconomyLoader(QConomy plugin) {
		try {
			Bukkit.getLogger().info("Economy Loader attempting to connect...");
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost/qcraft";
			connection = DriverManager.getConnection(url, "root", "paulisbest");
			Bukkit.getLogger().info("SQL Database Connection Successful!");
		}
		catch (Exception e) {
			Bukkit.getLogger().info("SQL Database Connection Failed!");
		}
		this.plugin = plugin;
	}

	public void loadPlayer(Player player) {
		String username = player.getName();
		try {
			String query = "SELECT balance FROM money WHERE username = ?";
			PreparedStatement statement = connection.prepareStatement(query);
			statement.setString(1, username);
			ResultSet data = statement.executeQuery();
			if (data.next()) {
				Money money = new Money(player, data.getInt("balance"));
				player.setMetadata("QConomy", new FixedMetadataValue(plugin, money));
			} 
			else {
				try {
					Money money = new Money(player, 0);
					player.setMetadata("QConomy", new FixedMetadataValue(plugin, money));
					query = "INSERT INTO money(username, amount) VALUES (? , ?)";
					statement = connection.prepareStatement(query);
					statement.setString(1, username);
					statement.setInt(2, 0);
					statement.executeUpdate();
				} 
				catch (SQLException e) {
					Bukkit.getLogger().info("Unable to generate money entry for: " + username);
				}
			}
		} 
		catch (SQLException e) {
			Bukkit.getLogger().info("Unable to retrieve money entry for: " + username);
		}
	}

	public void savePlayer(Player player) {
		Money money = (Money) player.getMetadata("QConomy").get(0).value();
		String username = player.getName();
		int balance = money.getBalance();
		String update = "UPDATE money SET balance = ? WHERE username = ?";
		try {
			PreparedStatement statement = connection.prepareStatement(update);
			statement.setInt(1, balance);
			statement.setString(2, username);
			statement.executeUpdate();
		} 
		catch (SQLException e) {
			Bukkit.getLogger().info("Unable to update money entry for: " + username);
		}
		player.removeMetadata("QConomy", plugin);
	}

	public void quit() {
		try {
			String update = "UPDATE money SET balance = ? WHERE username = ?";
			PreparedStatement statement = connection.prepareStatement(update);
			for (Player player : Bukkit.getOnlinePlayers()) {
				Money money = (Money) player.getMetadata("QConomy").get(0).value();
				String username = player.getName();
				int balance = money.getBalance();

				statement.setInt(1, balance);
				statement.setString(2, username);
				statement.addBatch();
				player.removeMetadata("QConomy", plugin);
			}
			statement.executeBatch();
			connection.close();
		}
		catch (SQLException e) {
			Bukkit.getLogger().info("Unable to update money entries");
		}
	}
}
